<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #mycanvas {
            width: 500px;
            height: 500px;
            border: 1px solid;
        }
    </style>
</head>

<body>
    <canvas id="mycanvas">

    </canvas>
    <script>
        // 预加载 ：图片，先加载图片 在绘制图片
        // let src = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fb7f61f10f35dba9348480e8525ad7123e58898e745fa-airRW4_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655877982&t=7844d432ce6335bfcd7894b524baaf34";

        // 1.一张图片的预加载；
        let canvas = document.querySelector("#mycanvas");
        let ctx = canvas.getContext("2d");
        // let img = new Image();
        // img.src = src;
        // // 预加载 ：等待图片加载完毕之后在实用
        // img.onload = function(){
        //     ctx.drawImage(img,0,0);
        // }

        // 2.多张图片的预加载 ：所有的图片全部加载完毕
        // let arr = ["https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fb7f61f10f35dba9348480e8525ad7123e58898e745fa-airRW4_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655877982&t=7844d432ce6335bfcd7894b524baaf34", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F063021120F9%2F210630120F9-1-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655878511&t=79b53dff66baf39d3be63507106d40df", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F210F2130512J47-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655878511&t=024e52e5f762f795c2a622131aa44bef"];

        let arrobj = {
            img1:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fb7f61f10f35dba9348480e8525ad7123e58898e745fa-airRW4_fw658&refer=http%3A%2F%2Fhbimg.b0.upaiyun.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655877982&t=7844d432ce6335bfcd7894b524baaf34",
            img2:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F063021120F9%2F210630120F9-1-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655878511&t=79b53dff66baf39d3be63507106d40df",
            img3:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp09%2F210F2130512J47-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655878511&t=024e52e5f762f795c2a622131aa44bef"
        }
// console.log(arrobj.length);


        // 预加载函数
        function loadImg(arr,cb) {
            let num = 0;
            let imgarr = {};
            let length = 0;
            for(let k in arr){
                length++;
            }

            // 循环对象
            for(let key in arr){
                let imgEle = new Image();
                imgEle.src = arr[key];
                imgEle.onload = function () {
                    num++;
                    // console.log(num, arr.length);
                    imgarr[key] = this;
                    // console.log(this);
                    if (num == length) {
                        // console.log(imgarr);
                        cb(imgarr);
                    }
                }
            }

            // arr.forEach(img => {
            //     let imgEle = new Image();
            //     imgEle.src = img;
            //     imgEle.onload = function () {
            //         num++;
            //         // console.log(num, arr.length);
            //         imgarr.push(this);
            //         // console.log(this);
            //         if (num == arr.length) {
            //             // console.log(imgarr);
            //             cb(imgarr);
            //         }
            //     }
            // })
        }
        loadImg(arrobj,function(imgarr){
            console.log(imgarr);
            ctx.drawImage(imgarr.img2,0,0);
        })



    </script>
</body>

</html>